﻿layui.use(['form'], function () {
    var $ = layui.jquery;
    var form = layui.form;
    let isValidate = true;
    //登陆校验
    //$.get('./../Login/loginCheck', function (data) {
    //    if (data != null) {
    //        location.href = data[0].门户域名;
    //    }
    //})
    //验证码
    function successCallback(res) {
        if (res.code === 0) {
            // 第一次验证通过
            $("#verify").text("验证通过");
            $("#verify")[0].className = "btn btn-success btn-block";
            isValidate = true;
        }
    }
    // 以下仅适用于 sim-captcha-js v0.1.0及以上版本
    window.simCaptcha = new SimCaptcha({
        element: document.getElementById("verify"),
        appId: "132132",
        callback: successCallback,
        baseUrl: "/",
        imgUrl: "api/vCode/VCodeImg",
        checkUrl: "api/vCode/VCodeCheck",
    });
    //登录
    form.on('submit(login)', function (data) {
        if (!isValidate) {
            layer.msg('请完成验证');
            return
        }
        let index = layer.load(1, { shade: [0.3, '#fff'] }), sdata = { 'password': data.field.longin_password }, result = $.encryptRequest({ data: sdata })
        data.field.longin_password = result.password;
        $.postJSON('/api/Login/getlogin', {
            'username': data.field.longin_username,
            'password': data.field.longin_password
        }, (data) => {
            layer.close(index);
            if (data.code == 500) {
                layer.msg(data.msg);
            } else {
                location.href = data.msg;
            }
        }) 
        return false;
    });
    //回车登录
    $(document).keyup(function (event) {
        if (event.keyCode == 13) {
            if (!isValidate) {
                layer.msg('请完成验证');
                return
            }
            $('#login').trigger("click");
        }
    });
})